package com.guofeng.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


// ReentrantLock 单应用加锁
@RestController
@RequestMapping("index")
public class IndexController3 {

    // 使用ReentrantLock锁解决单体应用的并发问题
    Lock lock = new ReentrantLock();

    @Autowired
    StringRedisTemplate template;

    @RequestMapping("/buy3")
    public synchronized String index() {
        String REDIS_LOCK ="good_lock";
        String value = UUID.randomUUID().toString().replace("-","");
        Boolean flag = template.opsForValue().setIfAbsent(REDIS_LOCK, value);
        template.expire(REDIS_LOCK,10, TimeUnit.SECONDS);

        lock.lock();

        try {
            String result = template.opsForValue().get("goods");
            int total = result == null ? 0 : Integer.parseInt(result);
            if (total > 0) {
                int realTotal = total - 1;
                template.opsForValue().set("goods", String.valueOf(realTotal));
                System.out.println("购买商品成功，库存还剩：" + realTotal + "件， 服务端口为8001");
                return "购买商品成功，库存还剩：" + realTotal + "件， 服务端口为8001";
            } else {
                System.out.println("购买商品失败，服务端口为8001");
            }
        } catch (NumberFormatException e) {
            lock.unlock();
        } finally {
            lock.unlock();
        }
        return "购买商品失败，服务端口为8001";
    }
}
